//
//  TRBuyCarTableViewCell.m
//  WeiMiLan
//
//  Created by anan on 15/8/3.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "TRBuyCarTableViewCell.h"

@implementation TRBuyCarTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSInteger)getHeight {
    return 130;
}

- (void)setInfo:(NSMutableDictionary *)info {
    _info = info;
    
    self.labelTitle.text = [info ql_stringForKey:@"DESCRIPTION"];
    [self.imageViewCover setImageWithURL:[NSURL URLWithString:[info ql_stringForKey:@"THUMBNAIL_URL"]] placeholderImage:[UIImage imageNamed:@"icon"]];
    
    self.labelNickname.text = [info[@"USER_INFO"] ql_stringForKey:@"NAME"];
    self.imageViewUserHeader.layer.masksToBounds = YES;
    self.imageViewUserHeader.layer.cornerRadius = 16; //设置图片圆角的尺度
    [self.imageViewUserHeader setImageWithURL:[NSURL URLWithString:[info[@"USER_INFO"] ql_stringForKey:@"HEAD_PIC_URL"]] placeholderImage:[UIImage imageNamed:@"nodata_userheader"]];

    self.labelPrize.text = [info ql_stringForKey:@"PRICE"];
    self.textFieldCount.text = [info ql_stringForKey:@"COUNT"];
    
    self.buttonSeclect1.selected = [self.info ql_boolForKey:@"select"];
    self.buttonSeclect2.selected = [self.info ql_boolForKey:@"select"];
}

- (IBAction)buttonAddAction:(UIButton*)sender {
    NSInteger count = [self.info ql_intForKey:@"COUNT"];
    [self.info ql_setInterge:count+1 forKey:@"COUNT"];
    
    self.info = _info;
    
    [self updateCount];
}

- (IBAction)buttonReduceAction:(UIButton*)sender {
    NSInteger count = [self.info ql_intForKey:@"COUNT"];
    if (count>1) {
        [self.info ql_setInterge:count-1 forKey:@"COUNT"];
    }
    
    self.info = _info;
    
    [self updateCount];
}

- (IBAction)buttonDeleteAction:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"确定删除此商品？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
    [alert show];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    [self updateCount];
    return YES;
}

- (IBAction)buttonSelectAction:(UIButton*)sender {
    [self.info ql_setObject:QLBoolToNumber(!sender.selected) forKey:@"select"];
    self.info = _info;
    [self.buyCarViewController updateCount];
}

- (void)updateCount {
    [QLMaskUtil showMaskLoadingWithView:self.buyCarViewController.view];
    NSString *URLTmp = @"http://ping.vsmu.cn/app/Interface/MyCart?type=11004&userId=%@&id=%@&count=%@";
    URLTmp = [NSString stringWithFormat:URLTmp,ApplicationDelegate.currentUserId,[self.info ql_stringForKey:@"CART_ID"],self.textFieldCount.text];
    NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码成UTF-8  否则可能会出现错误
    URLTmp = URLTmp1;
    NSLog(@"%@",URLTmp);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: URLTmp]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [QLMaskUtil hideLoadingMaskWithView:self.buyCarViewController.view];
        [self.buyCarViewController.contents removeObject:self.info];
        [self.buyCarViewController refresh];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [QLMaskUtil hideLoadingMaskWithView:self.buyCarViewController.view];
        
        NSLog(@"Failure: %@",error);
        [SVProgressHUD showImage:nil status:@"提交失败，请重试"];
    }];
    [operation start];

}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==1) {
        [QLMaskUtil showMaskLoadingWithView:self.buyCarViewController.view];
        NSString *URLTmp = @"http://ping.vsmu.cn/app/Interface/MyCart?type=11003&userId=%@&ids=%@";
        URLTmp = [NSString stringWithFormat:URLTmp,ApplicationDelegate.currentUserId,[self.info ql_stringForKey:@"CART_ID"]];
        NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码成UTF-8  否则可能会出现错误
        URLTmp = URLTmp1;
        NSLog(@"%@",URLTmp);
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: URLTmp]];
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            [QLMaskUtil hideLoadingMaskWithView:self.buyCarViewController.view];
            
//            NSString *requestTmp = [NSString stringWithString:operation.responseString];
//            NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
//            NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableContainers error:nil];
            [self.buyCarViewController.contents removeObject:self.info];
            [self.buyCarViewController refresh];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [QLMaskUtil hideLoadingMaskWithView:self.buyCarViewController.view];
            
            NSLog(@"Failure: %@",error);
            [SVProgressHUD showImage:nil status:@"提交失败，请重试"];
        }];
        [operation start];
    }
}

@end
